#include "graphicsViewGrid.h"
#include "componentTable.h"
#include "luaResources.h"
#include <QDebug>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QTableWidget>

GraphicsViewGrid::GraphicsViewGrid(QWidget *parent)
  : QGraphicsView(parent),
    mHighlightRect(0)
{
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setAlignment(Qt::AlignLeft|Qt::AlignTop);
  setScene(&mScene);
  mScene.setGridSize(20);

  clear();

  setAcceptDrops(true);
}

void GraphicsViewGrid::clear()
{
  mScene.clear();
  mHighlightRect = new QGraphicsPolygonItem();
  mHighlightRect->setPolygon(QPolygon(QRect(0, 0, mScene.gridSize(), mScene.gridSize())));
  mHighlightRect->setBrush(QColor(255, 0, 0, 100));
  mHighlightRect->hide();
  mScene.addItem(mHighlightRect);
  mPositionPixmaps.clear();
  mShipInventory.clear();
}

void GraphicsViewGrid::loadShip(const ShipInventory &inventory)
{
  clear();
  mShipInventory = inventory;
  LuaResources& lua(LuaResources::getLuaResources());
  PositionComponentNameMap components(mShipInventory.positionComponents());
  PositionComponentNameMap::const_iterator row(components.constBegin());
  for(; row != components.constEnd(); ++row)
  {
    ColumnComponentNameMap::const_iterator col(row.value().constBegin());
    for(; col != row.value().constEnd(); ++col)
    {
      QString name(col.value());
      addComponent(row.key(), col.key(), name, lua.graphics[name].pixmap);
    }
  }
}

ShipInventory GraphicsViewGrid::shipInventory() const
{
  return mShipInventory;
}

void GraphicsViewGrid::addComponent(unsigned int row, unsigned int col,
                                    const QString &componentName, const QPixmap& pixmap)
{
  unsigned int gridSize(mScene.gridSize());
  QPointF pixmapPos(col*gridSize, row*gridSize);

  QGraphicsPixmapItem* pixmapItem(mScene.addPixmap(pixmap));
  pixmapItem->setPos(pixmapPos);
  mPositionPixmaps[row][col] = pixmapItem;
  mShipInventory.addComponent(row, col, componentName);
}

void GraphicsViewGrid::dragEnterEvent(QDragEnterEvent *event)
{
  if (event->mimeData()->hasFormat("text/plain"))
  {
    event->acceptProposedAction();
  }
}

void GraphicsViewGrid::dragMoveEvent(QDragMoveEvent *event)
{
  unsigned int gridSize(mScene.gridSize());
  unsigned int top(static_cast<int>(event->pos().y()/gridSize)*gridSize);
  unsigned int left(static_cast<int>(event->pos().x()/gridSize)*gridSize);
  mHighlightRect->setPos(QPoint(left, top));
  mHighlightRect->show();
}

void GraphicsViewGrid::dragLeaveEvent(QDragLeaveEvent *event)
{
  mHighlightRect->hide();
}

void GraphicsViewGrid::dropEvent(QDropEvent *event)
{
  ComponentTable* table(dynamic_cast<ComponentTable*>(event->source()));
  if (table)
  {
    QPointF pixmapPos(mHighlightRect->pos());
    unsigned int gridSize(mScene.gridSize());
    addComponent(pixmapPos.y()/gridSize, pixmapPos.x()/gridSize,
                 event->mimeData()->text(),
                 qvariant_cast<QPixmap>(event->mimeData()->imageData()));
  }
  mHighlightRect->hide();
  event->accept();
}
